//
//  WKCustomAlertView.m
//  WeKeTang
//
//  Created by ichina on 2017/8/22.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKCustomAlertView.h"

@interface WKCustomAlertView ()

@end

@implementation WKCustomAlertView

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self){
        self.backgroundColor = [UIColor clearColor];
        UIView * view = [[UIView alloc]initWithFrame:self.bounds];
        view.backgroundColor = [UIColor blackColor];
        view.alpha = .7;
        view.layer.cornerRadius = 25;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        
        _titleLabel = [[YYLabel alloc] initWithFrame:CGRectMake((self.frame.size.width - 200) / 2, (self.frame.size.height - 40) / 2, 200, 40)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.numberOfLines = 2;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return self;
}

-(void)didMoveToSuperview{
    // 一定要在主线程中执行这个操作，否则可能会不显示弹出框
    [self performSelectorOnMainThread: @selector(hideView) withObject: nil waitUntilDone: NO];
}

-(void) viewDidHidden{
    
    [self performSelectorOnMainThread: @selector(removeFromSuperview) withObject: nil waitUntilDone: NO];
}

- (void)hideView{
    
    [UIView beginAnimations: @"alert" context:nil];
    [UIView setAnimationDelay: 1.0f];
    [UIView setAnimationDuration: 1.5f];
    self.alpha = 0;
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(viewDidHidden)];
    [UIView commitAnimations];
}

@end
